/**
 * 2016-4-8 下午02:38:01
 */
package com.carpo.info.util;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * 
 * @since jdk1.6
 * @date 2016-4-8
 *  
 */

public class ServiceUtil implements ApplicationContextAware {

	private static ApplicationContext ctx;
	
	/**
	 * 根据接口类型和名称获取对应实现类 
	 * @param <T>
	 * @param interfaceName
	 * @param name
	 * @return
	 */
	public static <T> T getServiceByTypeAndName(Class<T> interfaceName, String name){
		if(StringUtils.isEmpty(name)){
			return null;
		}
		Map<String, T> map = ctx.getBeansOfType(interfaceName);
		if(null != map){
			return map.get(name);
		}
		return null;
	}
	
	public static <T> Map<String, T> getServiceByType(Class<T> interfaceName){
		return ctx.getBeansOfType(interfaceName);
	}
	
	/** 
	  * {@inheritDoc}   
	  * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) 
	  */
	public void setApplicationContext(ApplicationContext ctx) throws BeansException {
		this.ctx = ctx;
	}

}
